#include "ProjectBlur.h"


ProjectBlur::ProjectBlur(const std::string &name, CC::Scene *scene)
	: ProjectSandbox(name, scene)
{
	CC_INFO("-----ProjectBlur----\n");
}

ProjectBlur::~ProjectBlur()
{
}

void ProjectBlur::OnAttach()
{
    ProjectSandbox::OnAttach();

    CC::Shader* shader = CC::ResourceManager::LoadShader("Renderer2D_QuadBlur", ("Renderer2D_QuadBlur"), ("Renderer2D_QuadBlur"));
    CC::ShaderComponent* shader_sc = new CC::ShaderComponent(shader);

    CC::Custom2DMaterialComponent* custom2DMaterialComponent = new CC::Custom2DMaterialComponent();
    custom2DMaterialComponent->datas["TextureSize0"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float2, GetWidth(), GetHeight());
    custom2DMaterialComponent->datas["BlurRadius"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float, 0.0f);
    custom2DMaterialComponent->datas["BlurDirection"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float2, 1.0f, 1.0f);
    
    GetNode()->AddComponent<CC::ShaderComponent>(shader_sc);
    GetNode()->AddComponent<CC::Custom2DMaterialComponent>(custom2DMaterialComponent);

    GetAnimObj()->SetAnimTime(4000.0);
}

void ProjectBlur::OnDetach()
{
    ProjectSandbox::OnDetach();
}

void ProjectBlur::OnUpdate(CC::Timestep ts, std::string renderscene)
{        
    ProjectSandbox::OnUpdate(ts, renderscene);

    if (0 == GetShowWin()) return;

    auto tran = GetNode()->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran->Translation = glm::vec3(0.0, 0.0, 0.0);
    tran->Scale = glm::vec3(GetWidth(), GetHeight(), 0.0);

    auto custom2DMaterialComponent = GetNode()->GetComponent<CC::Custom2DMaterialComponent>(CC::ComponentType_Custom2DMaterial);
    custom2DMaterialComponent->datas["TextureSize0"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float2, GetWidth(), GetHeight());
    custom2DMaterialComponent->datas["BlurRadius"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float, GetAnimObj()->GetCurrentValue());

}